package tool

import implicits.Implicits._
import shared.pojo.WorkflowMission.FsData
import utils.Utils

import java.io.File
import upickle.default._

/** Created by yz on 8/12/2020
  */
object FeatureSelectionTool {

  def getIdStrOp(fileName: String): Option[String] = {
    val names = fileName.mySplit("/")
    if (names.size > 1) {
      Some(names(0))
    } else {
      None
    }
  }

  def getIdStrOp(fileNameOp: Option[String]): Option[String] = {
    fileNameOp.flatMap { fileName =>
      getIdStrOp(fileName)
    }
  }

  def produceSubInputFile(workspaceDir: File, parentNodeStr: String) = {
    val diffDir = new File(workspaceDir.getParentFile, parentNodeStr)
    val diffMetaboliteFile = Tool.getDiffMetaboliteFile(diffDir)
    if (diffMetaboliteFile.exists()) {
      val metabolites = diffMetaboliteFile.csvLines.drop(1).map(_.head)
      val matrixFile = Tool.getMatrixFile(workspaceDir.getParentFile)
      matrixFile.txtLines.lineMapNoLowerData
        .filter { map =>
          metabolites.contains(map("Metabolite"))
        }
        .csvLines
        .toTxtFile(new File(workspaceDir, "AllMet_Raw.txt"))
    } else {
      List("Metabolite").toFile(new File(workspaceDir, "AllMet_Raw.txt"))
    }
  }

  def getFsData(workspaceDir: File) = {
    val value = Utils.configFile2Json(workspaceDir)
    value.transform(reader[FsData])
  }

}
